﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using System.Windows.Media;

namespace CoTime
{
    /// <summary>
    /// Interaction logic for PomoTimer.xaml
    /// </summary>
    public partial class PomoTimer : Window
    {
        #region [ Fields / Properties ]
        
        public Action MessageOk = null;
        public Action MessageCancel = null;

        private readonly DispatcherTimer _timer = new DispatcherTimer();
        private Int32 _remainMinutes = Config.MinutesOfPomoTime;
        public Boolean IsTimeOver
        {
            get;
            private set;
        }

        #endregion

        #region [ Constructor ]

        public PomoTimer()
        {
            InitializeComponent();
            _timer.Tick += new EventHandler(TimerTick);
            _timer.Tick += new EventHandler(TimerTick2);
            _timer.Interval = TimeSpan.FromMinutes(1.00);
            _timer.Start();
        }

        #endregion

        void TryFocusTimerTick(object sender, EventArgs e)
        {
            this.Focus();
            this.Topmost = true;
        }

        #region [ UI Animation ] 
        
        void TimerTick(object sender, EventArgs e)
        {
            _remainMinutes--;
            this.UILabelRemainingTime.Content = _remainMinutes.ToString();
            if (_remainMinutes == 0)
            {
                _timer.Stop();
                this.IsTimeOver = true;

                var tryFocusTimer = new DispatcherTimer();
                tryFocusTimer.Tick += new EventHandler(TryFocusTimerTick);
                tryFocusTimer.Interval = TimeSpan.FromSeconds(10);

                this.Focus();
                MessageBoxResult result = MessageBox.Show("时间到，休息一下吧O(∩_∩)O~", "Time out!", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.OK)
                {
                    MessageOk();
                }
                else if (result == MessageBoxResult.Cancel)
                {
                    MessageCancel();
                }
                this.Close();
            }
        }

        private Double _blackGradientStopOffset = 1.00f;

        void TimerTick2(object sender, EventArgs e)
        {
            UIRadialGradientBrush.GradientStops.Clear();
            _blackGradientStopOffset -= (1.00 / Config.MinutesOfPomoTime) / 2;
            var black = new GradientStop(Colors.Black, _blackGradientStopOffset);
            var transparent = new GradientStop(Colors.Transparent, 1.00);
            UIRadialGradientBrush.GradientStops.Add(black);
            UIRadialGradientBrush.GradientStops.Add(transparent);
        }

        private void UILabelRemainingTimeMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void WindowMouseUp(object sender, MouseButtonEventArgs e)
        {
            var mainWindow = Application.Current.MainWindow as MainWindow;
            if (mainWindow != null)
            {
                mainWindow.WindowState = mainWindow.WindowState == WindowState.Normal ? WindowState.Minimized : WindowState.Normal;
            }
        }
        #endregion

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            MessageOk = MessageCancel =  null;
            _timer.Stop();
        }

    }
}
